package com.cg.design.graphicdesignpattern.interpreter.language;

/**
 * 对应<primitive command>标签的类，继承了BaseNode类
 *
 * @program: LeetCode->PrimitiveCommandNode
 * @author: cg
 * @create: 2022-04-06 08:56
 **/
public class PrimitiveCommandNode extends BaseNode {

    private String name;

    private Executor executor;

    @Override
    public void parse(Context context) throws ParseException {
        name = context.currentToken();
        context.skipToken(name);
        executor = context.createExecutor(name);
    }

    @Override
    public void execute() throws ExecuteException {
        if (executor == null) {
            throw new ExecuteException(name + ": is not defined");
        } else {
            executor.execute();
        }
    }

    @Override
    public String toString() {
        return name;
    }
}
